package com.rr.security;

import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author RR
 * @date 2023/3/15 20:39
 */
/*
* token缓存管理器，简单模拟用户登录后生成token，以及通过token查找用户
* */
@Component
public class AuthTokenManager {
    // key:token value:username
    private Map<String, String> authMap = new HashMap<>();

    public String addToken(String username) {
        String token = DigestUtils.md5DigestAsHex(username.getBytes());
        authMap.put(token, username);
        return token;
    }

    public String getUsername(String token) {
        return authMap.get(token);
    }

    public void remove(String token) {
        authMap.remove(token);
    }
}
